package com.springboot2.controller;

import com.springboot2.entity.Org;
import com.springboot2.service.OrgService;
import com.springboot2.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("org/")
public class OrgController {
    @Resource
    private OrgService orgService;

    //增
    @PostMapping("add")
    public Result save(@RequestBody Org org){
        orgService.add(org);
        return Result.success();
    }
    //删
    @DeleteMapping("del/{id}")
    public Result del(@PathVariable int id){
        orgService.del(id);
        return Result.success();
    }
    //改
    @PutMapping("update")
    public Result update(@RequestBody Org org){
        orgService.update(org);
        return Result.success();
    }
    //获取对象
    @GetMapping("find/{id}")
    public Result findById(@PathVariable int id){
        return Result.success(orgService.findById(id));
    }
    //列表
    @GetMapping("list")
    public Result findAll(){
        Map<String,Object> map = new HashMap<>();
        map.put("data",orgService.findAll());
        map.put("total",orgService.count());
        return Result.success(map);
    }
    //查询条件
    @GetMapping("list/{name}")
    public Result findByName(@PathVariable String name){

        return Result.success(orgService.findByName(name));
    }

    //删
    @DeleteMapping("batch/{ids}")
    public Result dels(@PathVariable String ids){
        String [] splits = ids.split(",");
        for (String s:splits){
            orgService.del(Integer.parseInt(s));
        }
        return Result.success();
    }
}
